package vn.cuhanh;

import java.util.ArrayList;

import vn.cuhanh.adapter.GameListAdapter;
import vn.cuhanh.bean.Game;
import vn.cuhanh.fragment.MenuFragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class ChooseGameActivity extends Activity {
	
	public static ArrayList<Game> games = null;
	public static Game current = null;
	public static int currentPage = MenuFragment.PAGE_NEWS;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.choose_game);
		
		if(games == null || games.isEmpty()) {
			return;
		}
		
		GridView gridGames = (GridView) findViewById(R.id.gridGames);
		GameListAdapter gameListAdapter = new GameListAdapter(this, R.layout.choose_game_item, games);
		gridGames.setAdapter(gameListAdapter);
		gridGames.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				goToGameDetail(ChooseGameActivity.this, ChooseGameActivity.games.get(arg2));
			}
		});
	}
	
	@Override
	public void onResume() {
		if(games != null && games.size() == 1) {
			goToGameDetail(this, games.get(0));
		}
		super.onResume();
	}
	
	public static void goToGameDetail(Context ctx, Game game) {
		Intent intent = new Intent(ctx, GameDetailActivity.class);
		intent.putExtra("checkForZingMeInfoChanges", false);
		ChooseGameActivity.current = game;
		ctx.startActivity(intent);
	}
	
	public static int getPagePositionById(int pageId) {
		int position = 0;
		for (int i = 0; i < GameDetailActivity.availablePages.length; i++) {
			if(GameDetailActivity.availablePages[i] == pageId) {
				position = i;
				break;
			}
		}
		return position;
	}
}
